<?php
namespace app\index\controller;
use \app\check\controller\Check;
use \think\Db;
// 首页接口
class Index extends Check
{
    /**
     * 数据库连接  数量减少   只做一次连接
     * 连接数据库操作   因由服务发起
     * @param string $ucid
     * @param string $version
     * @return mixed
     */
    public function index($ucid='',$version='1.0.0')
    {
        $yunshi_arr = [
            'day'=>['id'=>1],
            'week'=>['id'=>4,'sub'=>[0,5,6,7,8,9,10,11,12,13,14,15,16]],
            'month'=>['id'=>17,'sub'=>[0,19,20,21,22,23,24,25,26,27,28,29,30]],
            'year'=>['id'=>18,'sub'=>[0,31,32,33,34,35,36,37,38,39,40,41,42]],
        ];
        $sun_article = [];
        $asc_article = [];

        $map['u.uc_id'] = $ucid;
        $map['r.isself'] = 1;

        $sun_asc = Db::table('wechat_backup_07_18_14_43_00.app_user')->alias('u')->field('sun,asc')
            ->join('wechat_backup_07_18_14_43_00.app_recode r','u.id=r.uid','LEFT')
            ->join('wechat_backup_07_18_14_43_00.app_recode_astro ra','r.id=ra.rid','INNER')
            ->where($map)
            ->find();
        // 存在自己的档案
        if(!empty($sun_asc))
        {
//            太阳星座
            $sun_in_sign = explode('-',$sun_asc['sun'])[0] * 1;
//            上升星座
            $asc_in_sign = explode('-',$sun_asc['asc'])[0] * 1;

            $cid1 = $yunshi_arr['week']['sub'][$sun_in_sign];
            $cid2 = $yunshi_arr['week']['sub'][$asc_in_sign];

            $map1['cid'] = $cid1;
            $map1['status'] = 1;


            $sun_article = Db::table('wechatadmin.system_article')
                ->field('id,cid,title,introduction,content,publish_time')
                ->where($map1)
                ->order('id DESC')
                ->find();

            $sun_article['in_sign'] = $sun_in_sign;

            if($cid1 == $cid2)
            {
                $asc_article = $sun_article;
            }
            else
            {
                $map2['cid'] = $cid2;
                $map2['status'] = 1;

                $asc_article = Db::table('wechatadmin.system_article')
                    ->field('id,cid,title,introduction,content,publish_time')
                    ->where($map2)
                    ->order('id DESC')
                    ->find();
            }
            $asc_article['in_sign'] = $asc_in_sign;

        }
        $btns = [];
        $index_button = Db::table('wechatadmin.system_index_button_cat')
            ->where(['status'=>1,'ver'=>$version])->find();

        if(!empty($index_button))
        {
            $bids = unserialize($index_button['bid']);
            $btns = Db::table('wechatadmin.system_index_button')
                ->where(['id'=>['in',$bids]])
                ->order('sort DESC')// 从大到小
                ->select();


        }
        // 首页轮播
        $slide = Db::table('wechatadmin.system_slide')
            ->field('id,cid,status,sort,name,description,link,target,image,content')
            ->where(['status'=>1,'cid'=>1])->order('sort DESC')->select();

        $category_thumb = Db::table('wechatadmin.system_category')
            ->field('id,name,thumb')
            ->where('id=2 or id=3')
            ->select();

        // 文章列表
        $map_article['a.cid'] = 1;
        $map_article['a.status'] = 1;

        $article_data = Db::table('wechatadmin.system_article')->alias('a')
            ->field('a.id,a.cid,a.title,a.introduction,u.author,u.avatar,a.thumb,a.publish_time,a.good,a.collect,(a.read+a.read_base) `read`,t.name type_name,t.name_en type_name_en')
            ->join('wechatadmin.system_admin_user u','a.uid=u.id','INNER')
            ->join('wechatadmin.system_article_type t','t.id=a.tid','LEFT')
            ->where($map_article)
            ->whereTime('a.publish_time', '<' ,time())// 查看当前时间以前的文章  预发布文章不显示
            ->order('a.publish_time DESC')
            ->paginate(10,false,['query'=>request()->param()]);

        // 添加 当前文章当前用户点赞 和 收藏状态
        $article_data = json_decode(json_encode($article_data),true);
        foreach ($article_data['data'] as $k => $d)
        {
            $article_data['data'][$k]['good_status'] = false;
            $article_data['data'][$k]['collect_status'] = false;
            //  点赞收藏
            $article_data['data'][$k]['good'] += floor($article_data['data'][$k]['read']/30);
            $article_data['data'][$k]['collect'] += floor($article_data['data'][$k]['read']/250);
            $good = Db::table('wechatadmin.system_article_good')->where(['aid'=>$d['id'],'ucid'=>$ucid])->find();

            $collect = Db::table('wechatadmin.system_article_collect')->where(['aid'=>$d['id'],'ucid'=>$ucid])->find();

            if($good != null) $article_data['data'][$k]['good_status'] = true;
            if($collect != null) $article_data['data'][$k]['collect_status'] = true;
        }

        return json([
            'code'=>200,
            'data'=>[
                'sun_article'=>$sun_article,
                'asc_article'=>$asc_article,
                'buttons'=>$btns,
                'slides'=>$slide,
                'category_thumb'=>$category_thumb,
                'article'=>$article_data,
                'lucky_planet_switch'=>true,// 幸运星功能开关
            ],
            'msg'=>'Success.'
        ]);

    }



}
